package com.nokia.example3dgame;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.media.*;
import javax.microedition.media.control.ToneControl;

public class GameSoundManager {

private Player[] gameSoundPlayers;
private Player[] gameMIDIPlayers;
private Player toneSequencePlayer;

public GameSoundManager(String[] gameMIDIFiles, String[] gameSoundsFiles) {
    if (gameSoundsFiles != null) {
        gameSoundPlayers = new Player[gameSoundsFiles.length];
        for (int i = 0; i < gameSoundsFiles.length; i++) {
            gameSoundPlayers[i] = loadSound("/" + gameSoundsFiles[i],
                    "audio/midi");
        }
    }

    if (gameMIDIFiles != null) {
        gameMIDIPlayers = new Player[gameMIDIFiles.length];
        for (int i = 0; i < gameMIDIFiles.length; i++) {
            gameMIDIPlayers[i] = loadSound("/" + gameMIDIFiles[i],
                    "audio/midi", -1);
        }
    }

    try {
        toneSequencePlayer = createToneSequencePlayer();
    } catch (Exception e) {
        toneSequencePlayer = null;
    }

}

public void playGameSound(int i) {
    if (gameSoundPlayers[i] != null) {
        playSound(gameSoundPlayers[i]);
    }
}

public void playMIDISound(int i) {
    if (gameMIDIPlayers != null) {
        playSound(gameMIDIPlayers[i]);
    }
}

public void stopMIDISound(int i) {
    if (gameMIDIPlayers != null) {
        stopSound(gameMIDIPlayers[i]);
    }

}

public void playToneSequence() {
    if (toneSequencePlayer != null) {
        playSound(toneSequencePlayer);
    }
}

public void close() {

    if (gameSoundPlayers != null) {
        for (int i = 0; i < gameSoundPlayers.length; i++) {
            gameSoundPlayers[i].close();
        }
    }
    if (gameMIDIPlayers != null) {
        for (int i = 0; i < gameMIDIPlayers.length; i++) {
            gameMIDIPlayers[i].close();
        }
    }
}

private Player loadSound(String fn, String type, int loopCount) {
    Player p = null;
    try {
        InputStream in = getClass().getResourceAsStream(fn);
        p = Manager.createPlayer(in, type);
        p.setLoopCount(loopCount);
        p.prefetch(); // Player is in the UNREALIZED state, it will implicitly call realize.
    } catch (Exception ex) {
        System.out.println("Could not load sound in " + fn);
    }
    return p;
}

private Player createToneSequencePlayer() throws IOException,
        MediaException {
    Player p = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
    p.realize();
    ToneControl c = (ToneControl) p.getControl("ToneControl");
    c.setSequence(createSequence());
    p.prefetch();
    return p;
}

private byte[] createSequence() {
    byte TEMPO = 30; // set tempo to 120 bpm, tempo is formed by multiplying the tempo modifier by 4
    byte volume = 100;
    byte d = 8; // eighth note
    byte C = ToneControl.C4;
    byte D = (byte) (C + 2);
    byte E = (byte) (C + 4);

    byte[] sequence = {
        ToneControl.VERSION, 1, // always 1
        ToneControl.TEMPO, TEMPO, // set the tempo
        ToneControl.SET_VOLUME, volume, // Set the new volume
        ToneControl.BLOCK_START, 0, // define block 0
        C, d, D, d, E, d, // define repeatable block of 3 eighth notes
        ToneControl.BLOCK_END, 0, // end block 0
        ToneControl.PLAY_BLOCK, 0, // play block 0
        ToneControl.SILENCE, d, E, d, D, d, C, d, // play some other
        ToneControl.PLAY_BLOCK, 0, // play block 0 again
    };
    return sequence;
}

private Player loadSound(String fn, String type) {
    return loadSound(fn, type, 1);
}

private void playSound(Player p) {
    if (p != null) {

        if (p.getState() == Player.PREFETCHED) {

            try {
                p.setMediaTime(0); // rewind (a safety precaution)
                p.start();
            } catch (Exception ex) {
					System.out.println("Could not play sound ");
            }
        }
    }
}

private void stopSound(Player p) {
    if (p != null) {
        if (p.getState() == Player.STARTED) {
            try {
                p.stop();
            } catch (Exception ex) {
                System.out.println("Could not stop sound ");
            }
        }
    }
}
}
